<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html>
<head>
<title>Qyoto dialogs</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="GUI, Qt, dialogs, Qyoto, Visual Basic">
<meta name="description" content="Qyoto dialogs">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<div class="content">


<a href="/" title="Home">Home</a>&nbsp;
<a href="..">Contents</a>


<h1>Qyoto dialogs</h1>

<p>
In this part of the Visual Basic Qyoto programming tutorial, we will work with dialogs. 
</p>

<div class="center"> 
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 
</div> 

<p>
Dialog windows or dialogs are an indispensable part of most modern GUI applications. 
A dialog is defined as a conversation between two or more persons. In a computer 
application a dialog is a window which is used to "talk" to the application. 
A dialog is used to input data, modify data, change the application settings etc. 
Dialogs are important means of communication between a user and a computer program. 
</p>


<h2>Message boxes</h2>

<p>
Message boxes are convenient dialogs that provide messages to the user of the application. 
The message consists of text and image data. 
</p>

<pre class="code">
' ZetCode Mono Visual Basic Qt tutorial
'
' This program shows
' QMessageBox dialogs
'
' author jan bodnar
' last modified May 2009
' website www.zetcode.com


Imports Qyoto


Public Class VBQApp 
    Inherits QWidget

    Public Sub New()
    
        Me.SetWindowTitle("Message boxes")
    
        Me.InitUI()
        
        Me.Resize(220, 90)
        Me.Move(300, 300)
        Me.Show()
        
    End Sub
    
    Private Sub InitUI()
    
        Dim grid As New QGridLayout(Me)
        grid.Spacing = 2

        Dim errb As New QPushButton("Error", Me)
        Dim warnb As New QPushButton("Warning", Me)
        Dim questb As New QPushButton("Question", Me)
        Dim infob As New QPushButton("Information", Me)
        Dim aboutb As New QPushButton("About", Me)

        grid.AddWidget(errb, 0, 0)
        grid.AddWidget(warnb, 0, 1)
        grid.AddWidget(questb, 1, 0)
        grid.AddWidget(infob, 1, 1)
        grid.AddWidget(aboutb, 2, 0)
        
        Connect(errb, SIGNAL("clicked()"), Me, SLOT("OnClicked()"))
        Connect(warnb, SIGNAL("clicked()"), Me, SLOT("OnClicked()"))
        Connect(questb, SIGNAL("clicked()"), Me, SLOT("OnClicked()"))
        Connect(infob, SIGNAL("clicked()"), Me, SLOT("OnClicked()"))
        Connect(aboutb, SIGNAL("clicked()"), Me, SLOT("OnClicked()"))
    
    End Sub

    &lt;Q_SLOT()&gt; _
    Private Sub OnClicked()

        Dim button As QPushButton = Sender()

        If "Error".Equals(button.Text())
            QMessageBox.critical(Me, "Error", "Error loading file!")
        Else If "Warning".Equals(button.Text())
            QMessageBox.warning(Me, "Warning", "Operation not permitted!")
        Else If "Question".Equals(button.Text())
            QMessageBox.question(Me, "Question", "Are you sure to quit?")
        Else If "Information".Equals(button.Text())
            QMessageBox.information(Me, "Information", "Download completed.")
        Else If "About".Equals(button.Text())
            QMessageBox.about(Me, "About", "ZetCode Qyoto Visual Basic tutorial.")        
        End If
       
    End Sub

    Public Shared Sub Main(ByVal args() As String)
        Dim qapp As New QApplication(args)
        Dim app As New VBQApp
        QApplication.Exec()
    End Sub

End Class
</pre>

<p>
We use the <b class="keyword">GridLayout</b> manager to set up a grid of
five buttons. Each of the buttons shows a different message box. 
</p>

<pre class="explanation">
Dim button As QPushButton = Sender()
</pre>

<p>
Here we determine, which button called the <b class="keyword">ShowDialog()</b> 
method. 
</p>

<pre class="explanation">
If "Error".Equals(button.Text())
    QMessageBox.critical(Me, "Error", "Error loading file!")
</pre>

<p>
In case we pressed the error button, we show the error dialog. 
We use static methods of the <b class="keyword">QMessageBox</b> class to show
the message boxes.  
</p>


<br>
<img src="/img/gui/vbqyoto/infodlg.png" alt="Information message dialog">&nbsp;&nbsp;
<img src="/img/gui/vbqyoto/warningdlg.png" alt="Warning message dialog">&nbsp;&nbsp;
<img src="/img/gui/vbqyoto/questiondlg.png" alt="Question message dialog">&nbsp;&nbsp;
<img src="/img/gui/vbqyoto/errordlg.png" alt="Error message dialog">&nbsp;&nbsp;
<img src="/img/gui/vbqyoto/aboutdlg.png" alt="About message dialog">
<br>
<br>



<h2>QInputDialog</h2>

<p>
The <b class="keyword">QInputDialog</b> class provides a simple convenience dialog to get a single 
value from the user. The input value can be a string, a number or an item 
from a list. A label must be set to tell the user what they should enter.
</p>

<pre class="code">
' ZetCode Mono Visual Basic Qt tutorial
'
' This program shows
' QInputDialog dialogs
'
' author jan bodnar
' last modified May 2009
' website www.zetcode.com


Imports Qyoto


Public Class VBQApp 
    Inherits QWidget

    Dim edit As QLineEdit

    Public Sub New()
    
        Me.SetWindowTitle("QInputDialog")
    
        Me.InitUI()
        
        Me.Resize(300, 150)
        Me.Move(300, 300)
        Me.Show()
        
    End Sub
    
    Private Sub InitUI()
    
        Dim show As New QPushButton("Dialog", Me)
        
        Connect(show, SIGNAL("clicked()"), Me, SLOT("ShowDialog()"))

        show.FocusPolicy = FocusPolicy.NoFocus
        show.Move(20, 20)

        edit = New QLineEdit(Me)
        edit.Move(130, 22)
    
    End Sub

    &lt;Q_SLOT()&gt; _
    Private Sub ShowDialog()

        Dim text As String = QInputDialog.GetText( _
                Me, "Input Dialog", "Enter your name")

        If text &lt;&gt; Nothing AndAlso text.Trim() &lt;&gt; String.Empty
            edit.SetText(text)
        End If
       
    End Sub

    Public Shared Sub Main(ByVal args() As String)
        Dim qapp As New QApplication(args)
        Dim app As New VBQApp
        QApplication.Exec()
    End Sub

End Class
</pre>

<p>
In the code example, we have a button and a line edit. The button shows
an input dialog. We get some text and the text is shown in the line edit widget. 
</p>

<pre class="explanation">
Dim text As String = QInputDialog.GetText( _
        Me, "Input Dialog", "Enter your name")
</pre>

<p>
The <b class="keyword">GetText()</b> static method creates the input dialog.
The text from the dialog is stored in the text variable. 
</p>

<pre class="explanation">
If text &lt;&gt; Nothing AndAlso text.Trim() &lt;&gt; String.Empty
    edit.SetText(text)
End If
</pre>

<p>
Before we update the line edit, we ensure, that the text variable is
not null and that it is not empty and does not consists only from spaces. 
</p>

<br>
<img src="/img/gui/vbqyoto/inputdlg.png" alt="Input dialog">
<div class="figure">Figure: Input dialog</div>


<h2>QColorDialog</h2>

<p>
The <b class="keyword">QColorDialog</b>  class provides a dialog widget for specifying 
colors. The color dialog's function is to allow users to choose colors.
</p>

<pre class="code">
' ZetCode Mono Visual Basic Qt tutorial
'
' In this program, we use the
' QColorDialog to change the color
' of a label text
'
' author jan bodnar
' last modified May 2009
' website www.zetcode.com


Imports Qyoto


Public Class VBQApp 
    Inherits QWidget

    Dim label As QLabel

    Public Sub New()
    
        Me.SetWindowTitle("QColorDialog")
    
        Me.InitUI()
        
        Me.Resize(300, 150)
        Me.Move(300, 300)
        Me.Show()
        
    End Sub
    
    Private Sub InitUI()
    
        label = New QLabel("ZetCode Qyoto Visual Basic tutorial", Me)

        Dim vbox As New QVBoxLayout(Me)
        label.Alignment = AlignmentFlag.AlignCenter
        vbox.AddWidget(label)
    
    End Sub
    
    Protected Overrides Sub MousePressEvent(ByVal e As QMouseEvent)

        Dim color As QColor = QColorDialog.GetColor()

        If Not color.IsValid() Then
            Return
        End If

        Dim style As String = String.Format("QWidget {{ color: {0} }}", _
            color.Name())
        label.SetStyleSheet(style)
        
    End Sub

    
    Public Shared Sub Main(ByVal args() As String)
        Dim qapp As New QApplication(args)
        Dim app As New VBQApp
        QApplication.Exec()
    End Sub

End Class
</pre>

<p>
We show a some text in the center of the window. By clicking on the area of the
window, we show a color dialog. We change the text foreground color to the selected color
from the dialog. 
</p>

<pre class="explanation">
Protected Overrides Sub MousePressEvent(ByVal e As QMouseEvent)
    ...
End Sub
</pre>

<p>
In order to receive mouse press events for our window, we must override the 
<b class="keyword">MousePressEvent()</b> method.
</p>

<pre class="explanation">
Dim color As QColor = QColorDialog.GetColor()
</pre>

<p>
The <b class="keyword">QColorDialog</b> is being created. The selected
color is stored in the <b class="keyword">color</b> variable.
</p>

<pre class="explanation">
If Not color.IsValid() Then
    Return
End If
</pre>

<p>
We do nothing, when the cancel button was pressed.
</p>

<pre class="explanation">
Dim style As String = String.Format("QWidget {{ color: {0} }}", _
    color.Name())
label.SetStyleSheet(style)
</pre>

<p>
Here we update the foreground color of the label's text.
</p>

<br>
<img src="/img/gui/vbqyoto/colordlg.png" alt="QColorDialog">
<div class="figure">Figure: QColorDialog</div>


<h2>QFontDialog</h2>

<p>
The <b class="keyword">QFontDialog</b> class provides a dialog widget for selecting a font.
</p>

<pre class="code">
' ZetCode Mono Visual Basic Qt tutorial
'
' In this program, we use the
' QFontDialog to change the font
' of a label text
'
' author jan bodnar
' last modified May 2009
' website www.zetcode.com


Imports Qyoto


Public Class VBQApp 
    Inherits QWidget

    Dim label As QLabel

    Public Sub New()
    
        Me.SetWindowTitle("QFontDialog")
    
        Me.InitUI()
        
        Me.Resize(300, 150)
        Me.Move(300, 300)
        Me.Show()
        
    End Sub
    
    Private Sub InitUI()
    
        label = New QLabel("ZetCode Qyoto Visual Basic tutorial", Me)

        Dim vbox As New QVBoxLayout(Me)
        label.Alignment = AlignmentFlag.AlignCenter
        vbox.AddWidget(label)
    
    End Sub
    
    Protected Overrides Sub MousePressEvent(ByVal e As QMouseEvent)

        Dim ok As Boolean = True
        
        Dim font As QFont = QFontDialog.GetFont(ok)
        
        If Not ok Then
            Return
        End If
        
        label.Font = font
        
    End Sub

    
    Public Shared Sub Main(ByVal args() As String)
        Dim qapp As New QApplication(args)
        Dim app As New VBQApp
        QApplication.Exec()
    End Sub

End Class
</pre>

<p>
This example is similar to the previous one. This time, we change
the font of the text. 
</p>

<pre class="explanation">
Dim font As QFont = QFontDialog.GetFont(ok)
</pre>

<p>
The <b class="keyword">QFontDialog</b> is being created. 
The boolean ok variable is set, when we press the OK button
of the dialog. 
</p>

<pre class="explanation">
If Not ok Then
    Return
End If
</pre>

<p>
We do nothing, if we didn't press the OK button.
</p>

<pre class="explanation">
label.Font = font
</pre>

<p>
The <b class="keyword">font</b> field stores the selected font. We update the label's font
to the newly selected font. 
</p>

<br>
<img src="/img/gui/vbqyoto/fontdlg.png" alt="QFontDialog">
<div class="figure">Figure: QFontDialog</div>

<hr class="btm">

<p>
In this part of the Visual Basic Qyoto tutorial, we worked with dialog windows. 
</p>

<div class="center"> 
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 
</div> 
<br> 


<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡ 
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified May 17, 2009  <span class="copyright">&copy; 2007 - 2012 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>

